<?php

namespace common\jobs\handle;

use Yii;
use common\helpers\ArrayHelper;
use common\models\order\Order;

/**
 * 订单通知队列操作
 *
 * @package common\jobs\handle
 * @property Order|null $model
 */
class NoticeHandle extends \common\jobs\base\BaseJobHandleModel {

    /**
     * @var integer 订单ID
     */
    public $order_id = null;

    /**
     * @var Order|null 商城订单
     */
    public $_model = null;

    /**
     * {@inheritdoc}
     */
    public function rules() {
        return [
            [['order_id'], 'required'],
            [['order_id'], 'integer'],
            [['order_id'], 'validateOrder'],
        ];
    }

    /**
     * 校验订单信息
     *
     * @param string $attribute the attribute currently being validated
     */
    public function validateOrder($attribute) {
        if ($this->hasErrors()) {
            return;
        }
        $model = Order::findOne($this->$attribute);
        if (!$model) {
            return $this->addError($attribute, '【' . $this->$attribute . '】订单不存在！');
        }
        if (!$model->isPaid()) {
            return $this->addError($attribute, '【' . $this->$attribute . '】订单未支付！');
        }
        if (!$model->hasVouchers()) {
            return $this->addError($attribute, '【' . $this->$attribute . '】无核销码！');
        }
        $this->_model = $model;
    }

    /**
     * 获取订单信息
     *
     * @return Order|null
     */
    public function getModel() {
        return $this->_model;
    }

    /**
     * 执行返佣
     *
     * @inheritdoc
     * @throws \yii\base\InvalidConfigException
     * @throws \common\components\wechat\components\WechatHttpException
     */
    public function save() {
        if (!$this->validate()) {
            return $this->dumpError('通知操作失败');
        }
        //获取订单信息
        $order = $this->getModel();
        $vouchers = $order->vouchers;
        $codes = ArrayHelper::getColumn($vouchers, 'code');
        $url = $order->getNoticeLink();
        $user = $order->user;
        $mobile = $user->decryptMobile(false);
        if (!$mobile) {
            $mobile = $order->source->reciverMobile;
        }
        Yii::error([
            'order' => $order->attributes,
        ]);
        //短信通知
        if ($mobile) {
            $content = Yii::t('sms', 'NOTICE_ORDER_VOUCHERS', [
                'title' => $order->title,
                'codes' => implode('，', $codes), //您成功订购海泉湾'.$titles.'，电子券编码为 '.$trade_no
            ]);
            Yii::error([
                'mobile' => $mobile,
                'content' => $content,
            ]);
            //Utils::sendsms($mobile, $content);
        }
        //模版消息通知
        $openid = $user->openid;
        Yii::error([
            'openid' => $openid,
            'url' => $url,
        ]);
        if ($openid) {
            $orderType = '';
            if ($order->model == Order::MODEL_BOOK_ROOM) {
                $orderType = '房间预订成功';
            } else {
                $orderType = '商品购买成功';
            }
            $templateMessage = [
                'touser' => $openid,
                'template_id' => Yii::t('sms', 'WECHAT_NOTICE_TID_PAYMENT'),
                'url' => $url,
                'data' => [
                    'first' => [
                        'value' => '恭喜您订单交易成功！',
                        'color' => '#173177',
                    ],
                    'keyword1' => [
                        'value' => $orderType,
                        'color' => '#173177',
                    ],
                    'keyword2' => [
                        'value' => $order->title,
                        'color' => '#173177',
                    ],
                    'keyword3' => [
                        'value' => $order->pay,
                        'color' => '#173177',
                    ],
                    'keyword4' => [
                        'value' => date('Y年m月d日 H:i', $order->updated_at),
                        'color' => '#173177',
                    ],
                    'keyword5' => [
                        'value' => $order->order_no,
                        'color' => '#173177',
                    ],
                    'remark' => [
                        'value' => '点击此消息可查看订单详情。',
                        'color' => '#173177',
                    ],
                //{{first.DATA}}
                //订单类型：{{keyword1.DATA}}
                //订单商品：{{keyword2.DATA}}
                //订单金额：{{keyword3.DATA}}
                //订单时间：{{keyword4.DATA}}
                //交易单号：{{keyword5.DATA}}
                //{{remark.DATA}}
                //恭喜您订单交易成功
                //订单类型：房间预订成功
                //订单商品：高级大床房
                //订单金额：368
                //订单时间：2014年7月21日 18:36
                //交易单号：3214545213523121423
                //感谢您的使用
                ]
            ];
            Yii::error([
                'templateMessage' => $templateMessage,
            ]);
            if (YII_ENV_PROD) {
                /* @var \common\components\wechat\MpWechat $wechat */
                $wechat = Yii::$app->get('wechat');
                $wechat->sendTemplateMessage($templateMessage);
            } else {
                Yii::error([
                    '本地环境无需自动下发',
                ]);
            }
        }
    }

}
